package com.lt.suangua.ui.fragment;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatCheckBox;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.github.gzuliyujiang.wheelpicker.DatimePicker;
import com.github.gzuliyujiang.wheelpicker.annotation.DateMode;
import com.github.gzuliyujiang.wheelpicker.annotation.TimeMode;
import com.github.gzuliyujiang.wheelpicker.contract.OnDatimePickedListener;
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity;
import com.github.gzuliyujiang.wheelpicker.entity.DatimeEntity;
import com.github.gzuliyujiang.wheelpicker.entity.TimeEntity;
import com.github.gzuliyujiang.wheelpicker.widget.DatimeWheelLayout;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.hjq.toast.ToastUtils;
import com.lt.base.BaseDialog;
import com.lt.suangua.BuildConfig;
import com.lt.suangua.R;
import com.lt.suangua.aop.SingleClick;
import com.lt.suangua.app.TitleBarFragment;
import com.lt.suangua.http.api.CalculateApi;
import com.lt.suangua.http.api.HomeApi;
import com.lt.suangua.http.glide.GlideApp;
import com.lt.suangua.http.glide.GlideRoundImage;
import com.lt.suangua.http.model.HttpData;
import com.lt.suangua.ui.activity.BrowserActivity;
import com.lt.suangua.ui.activity.HomeActivity;
import com.lt.suangua.ui.activity.LoginActivity;
import com.lt.suangua.utils.GlideUtils;
import com.lt.suangua.utils.SPUtils;
import com.lt.suangua.utils.UserInfoUtils;
import com.lt.suangua.widget.XCollapsingToolbarLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import okhttp3.Call;

public class PublishFragment extends TitleBarFragment<HomeActivity> implements View.OnClickListener {
    TextView tv_select_birthday;
    ImageView img_top, img_bottom;
    Button btn_submit;
    public EditText et_name;
    RadioGroup radiogroup;
    public int suantype;
    SmartRefreshLayout refreshLayout;

    public static PublishFragment newInstance() {
        return new PublishFragment();
    }

    @Override
    public boolean isStatusBarEnabled() {
        // 使用沉浸式状态栏
        return !super.isStatusBarEnabled();
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d("zoule","onresume");
      //  et_name.setText("");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_publish;
    }

    @Override
    protected void initView() {
        tv_select_birthday = findViewById(R.id.tv_select_birthday);
        img_top = findViewById(R.id.img_top);
        img_bottom = findViewById(R.id.img_bottom);
        btn_submit = findViewById(R.id.btn_submit);
        et_name = findViewById(R.id.et_name);
//        checkbox_man = findViewById(R.id.checkbox_man);
//        checkbox_woman = findViewById(R.id.checkbox_woman);
        radiogroup = findViewById(R.id.radiogroup);
        refreshLayout=findViewById(R.id.refreshLayout);
        radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rbt_man) {
                    sex = 1;
                } else if (checkedId == R.id.rbt_woman) {
                    sex = 2;
                }
            }
        });
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                initData();
            }
        });
        setOnClickListener(R.id.tv_select_birthday, R.id.btn_submit);
    }

    @SingleClick
    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_select_birthday) {
            onYearMonthDayTime();
        } else if (view.getId() == R.id.btn_submit) {
//            if(!UserInfoUtils.isLogin(getActivity())){
//                Intent intent = new Intent(getActivity(), LoginActivity.class);
//                startActivity(intent);
//                return;
//            }
            if (checkInfo()) {
                if (hour == 0) {
                    hour = 24;
                }
                String baseUrl = BuildConfig.HOST_URL + "api/result/index";
                String content = baseUrl + "?token=" + SPUtils.get(getActivity(), "token", "")
                        + "&suantype=" + suantype + "&name=" + name + "&sex=" + sex
                        + "&year=" + year + "&month=" + month + "&day=" + day + "&hour=" + hour;
                Log.d("tijiaodizhi", content);
                BrowserActivity.start(getAttachActivity(), content);
            }
        }
    }

    String name = "";
    int sex = 1;//1男2女
    String birthday = "";
    int year;
    int month;
    int day;
    int hour;

    public boolean checkInfo() {
        name = et_name.getText().toString();
        if (TextUtils.isEmpty(name)) {
            ToastUtils.show("请输入姓名");
            return false;
        }
        if (sex != 1 && sex != 2) {
            ToastUtils.show("请选择性别");
            return false;
        }
        if (TextUtils.isEmpty(birthday)) {
            ToastUtils.show("请选择出生日期");
            return false;
        }
        return true;
    }

    @Override
    public void initData() {
        EasyHttp.post(this)
                .api(new CalculateApi().setSuantype(suantype))
                .request(new HttpCallback<HttpData<CalculateApi.Bean>>(this) {


                    @Override
                    public void onSucceed(HttpData<CalculateApi.Bean> data) {

                        setData(data.getData());
                    }

                    @Override
                    public void onFail(Exception e) {
                        super.onFail(e);

                        Log.d("shuju", e.getMessage());
                    }

                    @Override
                    public void onEnd(Call call) {
                        super.onEnd(call);
                        refreshLayout.finishRefresh(300);
                    }
                });
    }



    void setData(CalculateApi.Bean bean) {
        suantype = bean.suantype;
        GlideUtils.loadLongImage(getActivity(),bean.headpic,img_top);
        GlideUtils.loadLongImage(getActivity(),bean.endpic,img_bottom);
    }

    public void onYearMonthDayTime() {
        DatimePicker picker = new DatimePicker(getActivity());
        final DatimeWheelLayout wheelLayout = picker.getWheelLayout();
        picker.setOnDatimePickedListener(new OnDatimePickedListener() {
            @Override
            public void onDatimePicked(int year, int month, int day, int hour, int minute, int second) {
                String text = year + "年" + month + "月" + day + "日 " + hour +"时";
//                text += wheelLayout.getTimeWheelLayout().isAnteMeridiem() ? " 上午" : " 下午";
                PublishFragment.this.year = year;
                PublishFragment.this.month = month;
                PublishFragment.this.day = day;
                PublishFragment.this.hour = hour;
                birthday = year + "-" + month + "-" + day + " " + hour;
                tv_select_birthday.setText(text);

                // Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();

            }
        });
        wheelLayout.setDateMode(DateMode.YEAR_MONTH_DAY);
        wheelLayout.setTimeMode(TimeMode.HOUR_24_NO_SECOND);
        DatimeEntity entity = new DatimeEntity();
        entity.setDate(DateEntity.target(1900,1,1));
        entity.setTime(TimeEntity.target(0,0,0));
        wheelLayout.setRange(entity, DatimeEntity.yearOnFuture(10));
        wheelLayout.setDateLabel("年", "月", "日");
        wheelLayout.setTimeLabel("时", "分", "秒");

        wheelLayout.getMinuteLabelView().setVisibility(View.GONE);
        wheelLayout.getMinuteWheelView().setVisibility(View.GONE);

        DatimeEntity entityDefault = new DatimeEntity();
        entityDefault.setDate(DateEntity.target(1990,1,1));
        entityDefault.setTime(TimeEntity.target(0,0,0));
        wheelLayout.setDefaultValue(entityDefault);
        picker.show();
    }
}
